#!/usr/bin/python3
# Copyright 2022 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Targets: https://github.com/advisories/GHSA-6j58-grhv-2769."""

import sys
import atheris
import pexpect
import pysecsan
import ansible_runner
from ansible_runner.config.runner import RunnerConfig

pysecsan.add_hooks()


def test_one_input(data):
  """Fuzzer entrypoint."""
  fdp = atheris.FuzzedDataProvider(data)

  conf = RunnerConfig('/tmp/')
  conf.suppress_ansible_output = True
  conf.expect_passwords = {pexpect.TIMEOUT: None, pexpect.EOF: None}
  conf.cwd = str('/tmp/')
  conf.env = {}
  conf.job_timeout = 10
  conf.idle_timeout = 0
  conf.pexpect_timeout = 2.
  conf.pexpect_use_poll = True
  conf.command = 'from_fuzzer'

  runner = ansible_runner.Runner(conf)
  runner.resource_profiling = True
  # rc.resource_profiling_base_cgroup = "; exec-san"
  assistance = True
  if assistance and fdp.ConsumeIntInRange(1, 100) > 80:
    conf.resource_profiling_base_cgroup = 'FROMFUZZ'
  else:
    conf.resource_profiling_base_cgroup = fdp.ConsumeUnicodeNoSurrogates(24)
  try:
    runner.run()
  except (RuntimeError, ValueError, TypeError) as _:
    pass


def main():
  """Set up and start fuzzing."""
  atheris.instrument_all()
  atheris.Setup(sys.argv, test_one_input, enable_python_coverage=True)
  atheris.Fuzz()


if __name__ == '__main__':
  main()
